//
//  YLLStatusListViewModel.swift
//  YLLSina
//
//  Created by 于玲玲 on 16/7/31.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class YLLStatusListViewModel: NSObject {
    
    
    lazy var list: [YLLHomeCellViewModel] = [YLLHomeCellViewModel]()
    
    
    
    func loadData(success: ()->(),failed: ()->()){
        
        // 1. url
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        
        // 2.拼接参数
        /*
         必选	类型及范围	说明
         access_token	true	string	采用OAuth授权方式为必填参数，OAuth授权后获得。
         */
        //        HMLoginViewModel.shareInstance.access_token
        
        guard let token = YLLLoginViewModel.shareInstance.access_token else {
            
            return
        }

        
        let params = ["access_token": token]
        
      
        
        // 3.
        YLLHTTPCient.shareInstance.request(.GET, URLString: urlString, parameters: params, progress: nil, success: { (_ , JSON) -> Void in
           printLog(JSON)
            
            guard let json = JSON as?[String :AnyObject] else{
                return
            }
            
            //初始化一个数组
            
            var temp: [YLLHomeCellViewModel] = [YLLHomeCellViewModel]()
            
            if let statuses = json["statuses"] as?[[String: AnyObject]]{
                
                for status in statuses{
                    
                    let statusModel = YLLStatusModel(dict: status)
                    
                    let viewModel = YLLHomeCellViewModel()
                    
                    viewModel.statusModel = statusModel
                    
                    temp.append(viewModel)
                    
                }
                
                self.list = temp
                
                
            }
            
            
            
            success()
            
           
            
        }) { (_ , error) -> Void in
            
          failed()
            
        }
        
    }

}
